package com.stms.service.impl;

import com.stms.dao.ContractRepository;
import com.stms.dao.DepotRepository;
import com.stms.dao.InvoiceRepository;
import com.stms.dao.TrucksRepository;
import com.stms.pojo.Invoice;
import com.stms.pojo.Purchase;
import com.stms.service.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Service
public class InvoiceServiceImpl implements InvoiceService {
    @Autowired
    private InvoiceRepository invoiceRepository;
    @Autowired
    private ContractRepository contractRepository;
    @Autowired
    private DepotRepository depotRepository;
    @Autowired
    private TrucksRepository trucksRepository;

    @Override
    public boolean addInvoice(Invoice invoice) {
        if (contractRepository.findByContractId(invoice.getContractId())==null||
                depotRepository.findByDepotId(invoice.getDepotId())==null||
                trucksRepository.findByLicense(invoice.getLicense())==null) {
            return false;
        }
        if (invoiceRepository.save(invoice)!=null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean editInvoice(Invoice invoice) {
        if (contractRepository.findByContractId(invoice.getContractId())==null||
                depotRepository.findByDepotId(invoice.getDepotId())==null||
                trucksRepository.findByLicense(invoice.getLicense())==null) {
            return false;
        }
        if (invoiceRepository.save(invoice)!=null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void deleteBatch(String invoiceIds) {
        String[] invoiceStr = invoiceIds.split(",");
        List<Integer> list = new ArrayList<>();

        for(String str:invoiceStr){
            list.add(Integer.valueOf(str));
        }
        invoiceRepository.deleteBatch(list);
    }

    @Override
    public Page<Invoice> fuzzyQuery(Integer pageNum, Integer pageSize, String direction, Invoice invoice) {
        if (pageNum==null||pageNum<=0){
            pageNum=1;
        }
        pageNum = pageNum-1;
        if (pageSize==null){
            pageNum=10;
        }
        if (direction==null){
            direction="ASC";
        }

        Date startDate = invoice.getStartDate();
        Date endDate = invoice.getEndDate();

        Sort.Direction direc = direction.equals("ASC")?Sort.Direction.ASC:Sort.Direction.DESC;
        Pageable pageable = PageRequest.of(pageNum,pageSize,direc,"id");

        Page<Invoice> page = invoiceRepository.findAll(new Specification<Invoice>(){
            @Override
            public Predicate toPredicate(Root<Invoice> root, CriteriaQuery<?> query, CriteriaBuilder cb){
                List<Predicate> list = new ArrayList<>();
                if (invoice==null){
                    return null;
                }
                if (invoice.getId()!=null&&!invoice.getId().equals("")){
                    list.add(cb.equal(root.get("id"),invoice.getId()));
                }
                if (invoice.getContractId()!=null&&!invoice.getContractId().equals("")){
                    list.add(cb.equal(root.get("contractId"),invoice.getContractId()));
                }
                if (invoice.getDepotId()!=null&&!invoice.getDepotId().equals("")){
                    list.add(cb.equal(root.get("depotId"),invoice.getDepotId()));
                }
                if (invoice.getLicense() != null && !invoice.getLicense().equals("")) {
                    list.add(cb.like(root.get("license").as(String.class), "%" + invoice.getLicense() + "%"));
                }
                if (startDate!=null&&!startDate.equals("")){
                    list.add(cb.greaterThanOrEqualTo(root.get("writeDate"),startDate));
                }
                if (endDate!=null&&!endDate.equals("")){
                    list.add(cb.lessThanOrEqualTo(root.get("writeDate"),endDate));
                }

                Predicate[] predicate = new Predicate[list.size()];
                return cb.and(list.toArray(predicate));
            }
        },pageable);
        return page;
    }


    @Override
    public Page<Invoice> findByPage(Integer pageNum, Integer pageSize) {
        if (pageNum==null||pageNum==0){
            pageNum=1;
        }
        pageNum = pageNum-1;
        if (pageSize==null){
            pageSize=10;
        }

        PageRequest of = PageRequest.of(pageNum,pageSize);
        Page<Invoice> page = invoiceRepository.findAll(of);
        return page;
    }
}
